package com.ruoyi.dynamic.api;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.huanxing.EMServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dynamic.domain.UserDynamic;
import com.ruoyi.dynamic.domain.UserParty;
import com.ruoyi.dynamic.pojo.AppUserDynamicVO;
import com.ruoyi.dynamic.service.AppDynamicService;
import io.swagger.annotations.Api;

@RestController
@RequestMapping("/api/v1/dynamic")
@Api(tags = "【APP】动态相关接口")
@CrossOrigin
public class AppDynamicController {

	@Autowired
	private AppDynamicService appDynamicService;
	@Autowired
	private EMServiceUtil eMServiceUtil;
	
	
	@GetMapping(value = "/test")
	public AjaxResult test(String formUserId,String toUserId,String content) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", content);
		eMServiceUtil.sendCustomMsg(formUserId, toUserId, "测试发送消息", map);
		return AjaxResult.success();
		
	}
	
	/**
	 * 	用户动态 
	 * @param dynamicType	1=我的动态 2=TA的主页动态  3=推荐动态 
	 * @param userId		dynamicType=2时必传
	 * @param pageNo		开始页
	 * @return
	 */
	@PostMapping(value = "/userDynamic")
	public AjaxResult userDynamic(@RequestBody AppUserDynamicVO param) {
		if(null == param) {
			return AjaxResult.error("参数为空");
		}
		if(null == param.getDynamicType()) param.setDynamicType(3);
		if(null == param.getUserId() && 2 == param.getDynamicType()) return AjaxResult.error("请选择用户");
		if(null == param.getPageNo()) param.setPageNo(1);
		return appDynamicService.userDynamic(param);
	}
	
	/**
	 * 	用户约玩
	 * @param dynamicType	1=我的约玩 2=TA的主页约玩  3=推荐约玩 
	 * @param type 			类型 1=双人约玩 2=派对
	 * @param userId		dynamicType=2时必传
	 * @param pageNo		开始页
	 * @return
	 */
	@PostMapping(value = "/userParty")
	public AjaxResult userParty(@RequestBody AppUserDynamicVO param) {
		if(null == param) {
			return AjaxResult.error("参数为空");
		}
		if(null == param.getDynamicType()) param.setDynamicType(3);
		if(null == param.getUserId() && 2 == param.getDynamicType()) return AjaxResult.error("请选择用户");
		if(null == param.getPageNo()) param.setPageNo(1);
		//if(null == param.getType()) param.setType(1);
		return appDynamicService.userParty(param);
	}
	
	/**
	 * 	删除动态
	 * @param param
	 * @return
	 */
	@PostMapping(value = "/delDynamic")
	public AjaxResult delDynamic(@RequestBody AppUserDynamicVO param) {
		if(null == param || null == param.getId()) {
			return AjaxResult.error("请选择动态");
		}
		return appDynamicService.delDynamic(param);
	}
	
	/**
	 * 	删除约玩
	 * @param param
	 * @return
	 */
	@PostMapping(value = "/delParty")
	public AjaxResult delParty(@RequestBody AppUserDynamicVO param) {
		if(null == param || null == param.getId()) {
			return AjaxResult.error("请选择约玩");
		}
		return appDynamicService.delParty(param);
	}

	/**
	 * 	发布动态
	 * @param param
	 * @return
	 */
	@PostMapping(value = "/publishDynamic")
	public AjaxResult publishDynamic(@RequestBody UserDynamic param) {
		if(null == param) {
			return AjaxResult.error("参数为空");
		}
		if(null == param.getType()) {
			return AjaxResult.error("类型不能为空");
		}
		if(StringUtils.isEmpty(param.getTitle())) {
			return AjaxResult.error("请输入动态标题");
		}
		if(StringUtils.isEmpty(param.getImg())) {
			return AjaxResult.error("请上传照片或视频再发布");
		}
		return appDynamicService.publishDynamic(param);
	}
	
	/**
	 * 	发布派对
	 * @param party
	 * @return
	 */
	@PostMapping(value = "/publishParty")
	public AjaxResult publishParty(@RequestBody UserParty party) {
		Assert.notNull(party, "参数为空");
		Assert.notNull(party.getType(), "类型不能为空");
		Assert.notNull(party.getTitle(), "请输入动态标题");
		Assert.notNull(party.getImg(), "请上传照片或视频再发布");
		Assert.notNull(party.getPartyTheme(), "请输入主题");
		Assert.notNull(party.getPartyInterest(), "请输入兴趣");
		Assert.notNull(party.getPayMethod(), "买单方式不能为空");
		if(null == party.getTimeType() || 0 == party.getTimeType()) {
			Assert.notNull(party.getBegTime(), "时间不能为空");
			Assert.notNull(party.getEndTime(), "时间不能为空");
		}
		Assert.notNull(party.getPartyType(), "约玩类型不能为空");
		return appDynamicService.publishParty(party);
	}
	
	/**
	 *	报名约玩
	 * @param party
	 * @return
	 */
	@PostMapping(value = "/enrollParty")
	public AjaxResult enrollParty(@RequestBody UserParty party) {
		Assert.notNull(party, "请选择约玩");
		Assert.notNull(party.getId(), "请选择约玩");
		return appDynamicService.enrollParty(party);
	}
	
	/**
	 * 	我的立即约玩列表
	 * @param partyType	 1=双人约玩 2=派对
	 * @param pageNo	开始页
	 * @return
	 */
	@GetMapping(value = "/promptlyParty")
	public AjaxResult promptlyParty(Integer partyType,Integer pageNo) {
		if(null == partyType) {
			partyType = 1;
		}
		if(null == pageNo) {
			pageNo = 1;
		}
		return appDynamicService.promptlyParty(partyType, pageNo);
	}
	
	/**
	 * 	约玩操作
	 * @param party
	 * @return
	 */
	@PostMapping(value = "/partyOperate")
	public AjaxResult partyOperate(@RequestBody UserParty party) {
		Assert.notNull(party, "请选择约玩");
		Assert.notNull(party.getId(), "请选择约玩");
		Assert.notNull(party.getUserId(), "请选择约玩");
		Assert.notNull(party.getType(), "请选择约玩");
		return appDynamicService.partyOperate(party);
	}
	
	/**
	 * 	发布权限控制 动态、活动
	 * @return
	 */
	@GetMapping(value = "/publishAuthControl")
	public AjaxResult publishAuthControl() {
		return appDynamicService.publishAuthControl();
	}
	
	
}
